package com.chenshang.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileWriterUtil {
	public static final Logger LOGGER = (Logger) LoggerFactory.getLogger(FileWriterUtil.class);

	public static void writeToFile(File file, String content) {
		if (!file.exists()) {
			try {
				file.createNewFile();
				LOGGER.info("文件不存在，创建文件");
			} catch (IOException e) {
				LOGGER.info("文件不存在，创建文件失败");
				e.printStackTrace();
			}
		}
		LOGGER.info("开始写入+" + file.getName() + " :" + content);
		FileWriter fw;
		BufferedWriter bw = null;
		try {
			fw = new FileWriter(file.getAbsoluteFile(), true);
			bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		LOGGER.info("写入完成");
	}

	public static void writeToFile(String pathname, String content) {
		File file = new File(pathname);
		FileWriterUtil.writeToFile(file, content);
	}
}
